#!/bin/bash
#
# Relinux is a remastering script based on remastersys
# It is made to be simpler, better, more customizable,
# better documented, up to date with the latest systems,
# more user friendly, and accessable by many linux distributions
#
# Copyright 2011/2012 under the GNU GPLv3 license
# Joel Leclerc (lkjoel)
#
# Original Script (remastersys) by: Tony Brijeski  
#
# A few useful variables
export DIR=/etc/relinux/
export VERSIONFILE=$DIR/relinux/version
export CONFIGFILE=$DIR/relinux.conf
# Load the version file
. $VERSIONFILE
# Prints out the usage of Relinux
usage()
{
	 thesudo=
	 command=
	 if [ `id -u` -ne 0 ]
	 then
	   thesudo="sudo "
	   command="exit 1"
	 fi
	 count=0
	 thspace=
	 progname=
	 while [ $count -lt ${#thesudo} ]
	 do
	   thspace="$thspace "
	   count=$(($count+1))
	 done
	 count=0
	 while [ $count -lt ${#PROGNAME} ]
	 do
	   progname="$progname "
	   count=$(($count+1))
	 done
     echo -e "Usage of $PROGNAME is as follows:"
     echo -e " "
     echo -e "   ${thesudo}$PROGNAME clean|iso|squashfs|onlyiso " \
"[configfile]"
     echo -e " "
     echo -e " "
     echo -e "Examples:"
     echo -e " "
     echo -e "   ${thesudo}$PROGNAME clean [configfile]              " \
" Cleans up files made by $PROGNAME"
     echo -e " "
     echo -e "   ${thesudo}$PROGNAME fullclean [configfile]          " \
" Cleans up files made by $PROGNAME, including the ISO file"
     echo -e "   ${thesudo}$PROGNAME squashfs [configfile]           " \
" Generates a .squashfs file from your system"
     echo -e " "
     echo -e "   ${thesudo}$PROGNAME onlyiso [configfile]            " \
" Generates a .iso file based on the .squashfs file generated by" \
" the previous listed command"
     echo -e "   ${thspace}$progname                                 " \
" If configfile is given, settings will be used from that file"
     echo -e " "
     echo -e "   ${thesudo}$PROGNAME iso [configfile]                " \
" Runs the last 2 listed commands"
     echo -e " "
     echo -e "   ${thesudo}$PROGNAME config                          " \
" Generates a configuration file in the current directory"
     $command
}
# Checks if a package is installed, and if not, install it.
checkbin()
{
	if [ $# -lt 3 ]
	then
	  if [ $# -lt 2 ]
	  then
	    app=$1
	    set $app $app $app
	  else
	    app=$1
	    package=$2
	    set $app $app $package
	  fi
	fi
	echo -en 'Checking for '"$2"' \t '
	which $1 > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
	  echo '[  OK  ]'
	else
	  echo '[ FAIL ]'
	  echo -en 'Installing   '"$2"' \t '
	  thesudo=
	  if [ `id -u` -ne 0 ]
	  then
	    thesudo="sudo "
	  fi
	  ${thesudo}apt-get install -y --force-yes -qq $3 > /dev/null 2>&1
	  if [ $? -eq 0 ]
	  then
	    echo '[  OK  ]'
	  else
	    echo '[ FAIL ]'
	  fi
	fi
}
# Checks if a package is installed by a file, and if not, install it
checkfile()
{
	echo -en 'Checking for '"$2"' \t '
	if [ -f $1 ]
	then
	  echo '[  OK  ]'
	else
	  echo '[ FAIL ]'
	  echo -en 'Installing  '"$2"' \t '
	  thesudo=
	  if [ `id -u` -ne 0 ]
	  then
	    thesudo="sudo "
	  fi
	  ${thesudo}apt-get install -y --force-yes -qq $3 > /dev/null 2>&1
	  if [ $? -eq 0 ]
	  then
	    echo '[  OK  ]'
	  else
	    echo '[ FAIL ]'
	  fi
	fi
}
# Checks if a package is installed
checkpackage()
{
	dpkg -l | awk '{print $2}' | grep '^'"$1"'$' > /dev/null 2>&1
	return $?
}
# Installs a package
installpackage()
{
	echo -en 'Installing  '"$2"' \t '
	apt-get install -y --force-yes -qq $1 > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
	  echo '[  OK  ]'
	else
	  echo '[ FAIL ]'
	  exit 1
	fi
}
# Removes a package
removepackage()
{
	echo -en 'Removing    '"$2"' \t '
	apt-get purge -y --force-yes -qq $1 > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
	  echo '[  OK  ]'
	else
	  echo '[ FAIL ]'
	  exit 1
	fi
}
# Boxes text
box()
{
	export count=${#1}
	export HASH=$(($count+6))
	printhash()
	{
		x=0
		while [ $x -lt $1 ]
		do
		  echo -n '#'
		  x=$(($x+1))
		done
	}
	printhash $HASH
	echo
	echo '## '$1' ##'
	printhash $HASH
	echo
}
# List file parser
parselist() {
	echo -n 'INCLUDE'
	cat $1 | grep '^+' | sed 's:^+ \(.*\):\1:g' | tr '\n' ' '
	echo
	echo -n 'EXCLUDE'
	cat $1 | grep '^-' | sed 's:^- \(.*\):\1:g' | tr '\n' ' '
	echo
}
# Simple uppercase conversion
toupper() {
	echo "$@" | tr '[:lower:]' '[:upper:]'
}
# Check if all apps needed are installed
checkbin bash "bash                " bash
checkbin sed "sed                 " sed
checkbin find "find                " findutils
checkbin passwd "passwd              " passwd
checkbin rsync "rsync               " rsync
checkbin mount "mount               " mount
checkbin eject "eject               " eject
checkfile /usr/lib/libdebian-installer.so.4.0.7 "libdebian-installer4" \
libdebian-installer4
checkfile /boot/memtest86+.bin "memtest86+           " memtest86+
checkbin casper-login "casper               " casper
checkbin mksquashfs "mksquashfs          " squashfs-tools
checkbin unsquashfs "unsquashfs          " squashfs-tools
checkbin genisoimage "genisoimage         " genisoimage
checkbin os-prober "os-prober           " os-prober
checkbin user-setup "user-setup           " user-setup
checkbin discover-config "discover-config     " discover
checkbin laptop-detect "laptop-detect       " laptop-detect
checkbin xterm "xterm               " xterm
checkbin syslinux "syslinux            " syslinux
checkbin fsck "util-linux           " util-linux
checkbin xresprobe "xresprobe           " xresprobe
checkbin archdetect "archdetect          " archdetect-deb
checkbin tree "tree                " tree
checkbin dialog "dialog              " dialog
checkbin lsinitramfs "initramfs-tools     " initramfs-tools
checkbin dirname "coreutils           " coreutils
checkbin dirname "casper           " casper
checkbin dirname "di-live           " di-live
# Check if the User configuration file is loaded
if [ $# -lt 2 ]
then
  export USERCONFIGFILE=$CONFIGFILE
else
  export USERCONFIGFILE=$2
fi
# If the user is not root, then give usage message
if [ `id -u` -ne 0 ]; then
     usage
fi
# Load the relinux configuration file for defaults
. $CONFIGFILE
# Load the user configuration file
. $USERCONFIGFILE
# Making sure that the correct options are selected
case $1  in
  clean)
     #
     ;;
  iso)
     # 
     ;;
  squashfs)
     #
     ;;
  onlyiso)
     #
     ;;
  fullclean)
     #
     ;;
  config)
     #
     ;;
  *)
     usage
     exit 1
     ;;
esac
# Generate the SquashFS file
cdfs() {
  ############
  ## STEP 1 ##
  ############
  # Install/Remove all packages needed automatically
  #
  box 'STEP 1 of SquashFS'
  echo 'Installing/Removing all packages needed to install/remove'
  # Remove popularity-contest as it can break ubiquity
  checkpackage popularity-contest && removepackage popularity-contest \
"Popularity Contest"
  ############
  ## STEP 2 ##
  ############
  #
  # Create CD tree in $WORKDIR
  box 'STEP 2 of SquashFS'
  echo 'Creating CD tree in '"$WORKDIR"''
  if [ -d "$WORKDIR" ]; then
    rm -rf $WORKDIR/dummysys/var/*
    rm -rf $WORKDIR/dummysys/etc/*
    rm -rf $WORKDIR/ISOTMP/isolinux
    rm -rf $WORKDIR/ISOTMP/grub
    rm -rf $WORKDIR/ISOTMP/.disk
  else
    mkdir -p $WORKDIR/ISOTMP/casper
    mkdir -p $WORKDIR/ISOTMP/preseed
    mkdir -p $WORKDIR/dummysys/dev
    mkdir -p $WORKDIR/dummysys/etc
    mkdir -p $WORKDIR/dummysys/proc
    mkdir -p $WORKDIR/dummysys/tmp
    mkdir -p $WORKDIR/dummysys/sys
    mkdir -p $WORKDIR/dummysys/mnt
    mkdir -p $WORKDIR/dummysys/media/cdrom
    mkdir -p $WORKDIR/dummysys/var
    chmod ug+rwx,o+rwt $WORKDIR/dummysys/tmp
  fi
  mkdir -p $WORKDIR/ISOTMP/isolinux
  mkdir -p $WORKDIR/ISOTMP/.disk
  echo "Done"
  tree $WORKDIR
  ############
  ## STEP 3 ##
  ############
  #
  # Copy /var and /etc to $WORKDIR/dummysys, and exclude $EXCLUDES
  box 'STEP 3 of SquashFS'
  echo "Copying /etc and /var to a temporary directory, "\
"so that we can work on them"
  EXCLUDES="$EXCLUDES *.pid boot/grub*"
  if [[ "$KEEPRELINUX" == "N" ]]
  then
    EXCLUDES="$EXCLUDES etc/relinux* usr/bin/relinux "\
"usr/doc/relinux*"
  fi
  if [[ "$KEEPLOG" == "N" ]]
  then
    EXCLUDES="$EXCLUDES *.log.* *.log"
  fi
  if [[ "$KEEPBACKUP" == "N" ]]
  then
    EXCLUDES="$EXCLUDES *.bak"
  fi
  if [[ "$KEEPOLD" == "N" ]]
  then
    EXCLUDES="$EXCLUDES *.[0-9].gz *.cache Cache *.bash_history"\
".thumbnails /tmp*"
  fi
  if [[ "$KEEPDEB" == "N" ]]
  then
    EXCLUDES="$EXCLUDES *.deb"
  fi
  if [[ "$BACKUPMODE" == "N" ]]
  then
    EXCLUDES="$EXCLUDES home"
  fi
  if [[ "$KEEPWORKDIR" == "N" ]]
  then
    TEMP=`echo $WORKDIR | sed 's:^.::g'`
    EXCLUDES="$EXCLUDES $TEMP"
  fi
  if [ "$EXCLUDES" != "" ]
  then
    for addvar in $EXCLUDES ; do
      VAREXCLUDES="$VAREXCLUDES --exclude='$addvar' "
    done
  fi
  rsync $VAREXCLUDES -a /var/. $WORKDIR/dummysys/var/.
  rsync $VAREXCLUDES -a /etc/. $WORKDIR/dummysys/etc/.
    rm -rf $WORKDIR/dummysys/etc/udev/rules.d/70-persistent*
  echo "Done. Removing unneeded files, and changing others"
  ls $WORKDIR/dummysys/var/lib/apt/lists | grep -v ".gpg" | \
grep -v "lock" | grep -v "partial" | \
xargs -i rm $WORKDIR/dummysys/var/lib/apt/lists/{} ; 

  find $WORKDIR/dummysys/var/run $WORKDIR/dummysys/var/log \
$WORKDIR/dummysys/var/mail $WORKDIR/dummysys/var/spool \
$WORKDIR/dummysys/var/lock $WORKDIR/dummysys/var/backups \
$WORKDIR/dummysys/var/tmp $WORKDIR/dummysys/var/crash \;
  for i in dpkg.log lastlog mail.log syslog auth.log daemon.log \
faillog lpr.log mail.warn user.log boot debug mail.err messages wtmp \
bootstrap.log dmesg kern.log mail.info
  do
    touch $WORKDIR/dummysys/var/log/${i}
  done
  echo "Done"
  ############
  ## STEP 4 ##
  ############
  #
  # Setup ISOLINUX
  #
  # Make sure that adduser and autologin have the correct permissions
  box 'STEP 4 of SquashFS'
  echo "Setting up ISOLINUX"
  [ ! -d $WORKDIR/dummysys/home ] && mkdir $WORKDIR/dummysys/home
  chmod 755 /usr/share/initramfs-tools/scripts/casper-bottom/*adduser \
/usr/share/initramfs-tools/scripts/casper-bottom/*autologin 
  # Copy the Preseed files
  cp $PRESEED $WORKDIR/ISOTMP/preseed/
  # Copy MemTest86+
  cp /boot/memtest86+.bin $WORKDIR/ISOTMP/isolinux/memtest
  # Check for some files that can come in handy
  if [ ! -f $DIR/customisolinux/isolinux.cfg ]; then
    find /usr -name 'isolinux.bin' -exec cp {} \
$WORKDIR/ISOTMP/isolinux/ \;
    find /usr -name 'vesamenu.c32' -exec cp {} \
$WORKDIR/ISOTMP/isolinux/ \;
    # System version, e.g. 11.10
    SYSVERSION=`lsb_release -rs`
    sed -e 's/__LIVECDLABEL__/'"$LIVECDLABEL"'/g' \
$ISOLINUX > $WORKDIR/ISOTMP/isolinux/isolinux.cfg
    # Add the splash image
    # Sed script simply renames the file, while keeping the extension
    ZASPLASHIMAGE=`echo \`basename $SPLASHIMAGE\` | \
sed 's:.*\(\..*\):splash\1:g'`
    cp $SPLASHIMAGE $WORKDIR/ISOTMP/isolinux/$ZASPLASHIMAGE
    sed -i 's/__SPLASHIMAGE__/'"$ZASPLASHIMAGE"'/g' \
$WORKDIR/ISOTMP/isolinux/isolinux.cfg
    sed -i 's/__TIMEOUT__/'"$LIVECDTIMEOUT"'/g' \
$WORKDIR/ISOTMP/isolinux/isolinux.cfg
  else
    cp $DIR/customisolinux/* $WORKDIR/ISOTMP/isolinux/
  fi
  # Checks the architecture of the system e.g. i386 or amd64
  ARCH=`archdetect | awk -F "/" '{print $1}'`
  cat > $WORKDIR/ISOTMP/README.diskdefines << EOF
#define DISKNAME  $LIVECDLABEL - Release $ARCH
#define TYPE  binary
#define TYPEbinary  1
#define ARCH  $ARCH
#define ARCH$ARCH  1
#define DISKNUM  1
#define DISKNUM1  1
#define TOTALNUM  0
#define TOTALNUM0  1
EOF
  cp $WORKDIR/ISOTMP/README.diskdefines \
$WORKDIR/ISOTMP/casper/README.diskdefines
  # Make filesystem.manifest (package list)
  echo "Creating filesystem.manifest and filesystem.manifest-desktop"
  dpkg-query -W --showformat='${Package} ${Version}\n' \
        > $WORKDIR/ISOTMP/casper/filesystem.manifest
  # Remove unwanted packages
  for i in $REMOVEAFTERINSTALL
  do
    # Notice that I used # as the delimiter, as /, : and = are used
    # in Apt and Aptitude
    sed -i 's#'"$i"'##g' $WORKDIR/ISOTMP/casper/filesystem.manifest
  done
  cp $WORKDIR/ISOTMP/casper/filesystem.manifest \
$WORKDIR/ISOTMP/casper/filesystem.manifest-desktop
  if [[ "$GENERATECASPER" == "Y" ]]
  then
    if [[ "$GENERATELSBRELEASE" == "Y" ]]
    then
      echo "Setting Casper,  and LSB_RELEASE options"
    else
      echo "Setting Capser, options"
    fi
  else
    if [[ "$GENERATELSBRELEASE" == "Y" ]]
    then
      echo "Setting  LSB_RELEASE options"
    else
      echo "Setting options"
    fi
  fi
  if [[ "$GENERATECASPER" == "Y" ]]
  then
    # Make casper.conf
    echo "# This file should go in /etc/casper.conf" > \
$WORKDIR/dummysys/etc/casper.conf
    echo "# Supported variables are:" >> \
$WORKDIR/dummysys/etc/casper.conf
    echo "# USERNAME, USERFULLNAME, HOST, BUILD_SYSTEM" >> \
$WORKDIR/dummysys/etc/casper.conf
    echo " " >> $WORKDIR/dummysys/etc/casper.conf
    echo "export USERNAME=\"$LIVEUSER\"" >> \
$WORKDIR/dummysys/etc/casper.conf
    echo "export USERFULLNAME=\"$LIVENAME\"" >> \
$WORKDIR/dummysys/etc/casper.conf
    echo "export HOST=\"$HOSTNAME\"" >> \
$WORKDIR/dummysys/etc/casper.conf
    echo "export BUILD_SYSTEM=\"$SYSNAME\"" >> \
$WORKDIR/dummysys/etc/casper.conf
  fi
  if [[ "GENERATELSBRELEASE" == "Y" ]]
  then
    # Make lsb-release
    echo "DISTRIB_ID=\"$SYSNAME\"" > $WORKDIR/dummysys/etc/lsb-release
    echo "DISTRIB_RELEASE=\"$SYSVERSION\"" >> \
$WORKDIR/dummysys/etc/lsb-release
    echo "DISTRIB_CODENAME=\"$SYSCODE\"" >> \
$WORKDIR/dummysys/etc/lsb-release
    echo "DISTRIB_DESCRIPTION=\"$SYSDESC\"" >> \
$WORKDIR/dummysys/etc/lsb-release
  fi
  # Make a new initial ramdisk including the casper scripts
  mkinitramfs -o $WORKDIR/ISOTMP/casper/initrd.gz `uname -r`
  echo "Copying your kernel and initrd to the livecd"
  cp /boot/vmlinuz-`uname -r` $WORKDIR/ISOTMP/casper/vmlinuz
  # Make filesystem.squashfs
  if [ -f $WORKDIR/ISOTMP/casper/filesystem.squashfs ]
  then
    rm -f $WORKDIR/ISOTMP/casper/filesystem.squashfs
  fi
  ############
  ## STEP 5 ##
  ############
  #
  # Create SquashFS file
  box 'STEP 5 of SquashFS'
  echo "Creating filesystem.squashfs"
  echo "This will take a while so be patient"
  SQUASHFSOPTSHIGH="-no-recovery -always-use-fragments"
  echo "Adding stage 1 files/folders that the livecd requires."
  # add the blank folders and trimmed down /var to the cd filesystem
  mksquashfs $WORKDIR/dummysys/ \
$WORKDIR/ISOTMP/casper/filesystem.squashfs -b 1M -no-duplicates \
$SQUASHFSOPTSHIGH > /dev/null 2>&1
  echo "Adding stage 2 files/folders that the livecd requires."
  echo "This will probably take longer than the first stage."
  # Add the rest of the system
    mksquashfs / $WORKDIR/ISOTMP/casper/filesystem.squashfs -b 1M \
-no-duplicates $SQUASHFSOPTSHIGH -e .thumbnails .cache .bash_history \
Cache boot/grub dev etc media mnt proc sys var \
$WORKDIR $EXCLUDES > /dev/null 2>&1
  # Make sure that the SquashFS file is under the iso9660 specification
  SQUASHFSSIZE=`wc -c $WORKDIR/ISOTMP/casper/filesystem.squashfs | \
awk -F " " '{print $1}'`
  if [ "$SQUASHFSSIZE" -ge "4294967296" ]
  then
    echo " The compressed filesystem is larger than the iso9660 "\
"specification allows for a single file. You must try to reduce the "\
"amount of data you are making and try again."
    exit 1
  fi
  # Add filesystem size
  echo "Calculating the installed filesystem size for the installer"
  unsquashfs -lls $WORKDIR/ISOTMP/casper/filesystem.squashfs | \
grep -v " inodes " | grep -v "unsquashfs:" | \
awk '{print $3}' | grep -v "," > /tmp/size.tmp
  for i in `cat /tmp/size.tmp`
  do
    a=$(($a+$i))
  done
  echo $a > $WORKDIR/ISOTMP/casper/filesystem.size
  echo "Done"
  echo "Creating MD5 sum"
  md5sum $WORKDIR/ISOTMP/casper/filesystem.squashfs > \
$WORKDIR/.filesystem.squashfs.md5
  touch $WORKDIR/.filesystem.squashfs.GENERATED.BY.RELINUX.PROOF
  echo "Done"
  echo "Finished generating SquashFS file."
}
# Generate the ISO file from the function above
iso() {
  ############
  ## STEP 1 ##
  ############
  #
  # Detect tools for ISO Generation
  box 'STEP 1 of ISO Generation'
  echo "Detecting your tools for ISO generation"
  CREATEISO="`which genisoimage`"
  if [ "$CREATEISO" = "" ]; then
    CREATEISO="`which mkisofs`"
  fi
  echo "Done"
  ############
  ## STEP 2 ##
  ############
  #
  # Verify the SquashFS file
  box 'STEP 2 of ISO Generation'
  echo "Verifying your SquashFS file"
  # check to see if the cd filesystem exists
  if [ ! -f "$WORKDIR/ISOTMP/casper/filesystem.squashfs" ]; then
    echo "The CD filesystem is missing.  Either there was a problem "\
"creating the compressed filesystem or you are trying to run "\
"sudo relinux only before sudo relinux squashfs"
    exit 1
  fi
  if [ ! -f "$WORKDIR/.filesystem.squashfs.GENERATED.BY.RELINUX.PROOF" ]
  then
    echo "The CD filesystem is not made by Relinux, and therefore, "\
"cannot be used. Run 'sudo relinux squashfs [configfile]' "\
"(without quotes, and replace configfile with your configuration file)"\
" to fix this problem."
    exit 1
  fi
  if [ ! -f "$WORKDIR/.filesystem.squashfs.md5" ]
  then
    echo "No MD5 Sums are present. Run 'sudo relinux squashfs "\
"[configfile]' (without quotes, and replace configfile with your "\
"configuration file) to fix this problem."
    exit 1
  else
    md5sum -c $WORKDIR/.filesystem.squashfs.md5 > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
      echo "SquashFS file does not match MD5 sum. Run 'sudo relinux "\
"squashfs [configfile]' (without quotes, and replace configfile with "\
"your configuration file) to fix this problem."
      exit 1
    fi
  fi
  # Make sure that the SquashFS file is under the iso9660 specification
  SQUASHFSSIZE=`wc -c $WORKDIR/ISOTMP/casper/filesystem.squashfs | \
awk -F " " '{print $1}'`
  if [ "$SQUASHFSSIZE" -ge "4294967296" ]; then
    echo " The compressed filesystem is larger than the iso9660 "\
"specification allows for a single file. You must try to reduce the "\
"amount of data you are making and try again."
    exit 1
  fi
  echo "Done. No errors."
  ############
  ## STEP 3 ##
  ############
  #
  # Make the ISO compatible with Ubuntu Startup Disk Creator
  box 'STEP 3 of ISO Generation'
  echo "Making disk compatible with Ubuntu Startup Disk Creator."
  touch $WORKDIR/ISOTMP/ubuntu
  touch $WORKDIR/ISOTMP/.disk/base_installable
  echo "full_cd/single" > $WORKDIR/ISOTMP/.disk/cd_type
  ARCH=`archdetect | awk -F "/" '{print $1}'`
  echo "$LIVECDLABEL - Release $ARCH" > $WORKDIR/ISOTMP/.disk/info
  echo "$LIVECDURL" > $WORKDIR/ISOTMP/.disk/release_notes_url
  ############
  ## STEP 4 ##
  ############
  #
  # Make the MD5 Sums
  box 'STEP 4 of ISO Generation'
  echo "Making temporary MD5 Sums for generation of the ISO file"
  cd $WORKDIR/ISOTMP && find . -type f -print0 | xargs -0 md5sum > \
md5sum.txt
  # remove files that change and cause problems with checking the disk
  sed -i 's/.*isolinux.*//g' md5sum.txt
  sed -i 's/.*md5sum.*//g' md5sum.txt
  ############
  ## STEP 5 ##
  ############
  #
  # Create the ISO file and MD5 sum
  box 'STEP 5 of ISO Generation (final step)'
  echo "Creating the ISO file and MD5 sum for it"
  echo "Creating $CUSTOMISO in $WORKDIR"
  $CREATEISO -quiet -r -V "$LIVECDLABEL" -cache-inodes -J -l \
-b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot \
-boot-load-size 4 -boot-info-table -o $WORKDIR/$CUSTOMISO \
"$WORKDIR/ISOTMP" > /dev/null 2>&1
  # Create the MD5 sum
  echo "Creating $CUSTOMISO.md5 in $WORKDIR"
  cd $WORKDIR
  md5sum $CUSTOMISO > $CUSTOMISO.md5
  sleep 1
  echo "Done"
  echo
  echo "$WORKDIR/$CUSTOMISO is generated. It is recommended to run it"\
" inside a virtual machine before you release it."
  echo
  echo "The size of $WORKDIR/$CUSTOMISO is "\
"`ls -hs $WORKDIR/$CUSTOMISO | awk '{print $1}'`"
  ISOSIZE=`wc -c $WORKDIR/$CUSTOMISO | awk '{print $1}'`
  if [ "$ISOSIZE" -ge "734003200" ]
  then
    echo "You will need to burn a DVD/USB/Blu-Ray disk instead of a CD."
  fi
  echo
  echo "It is recommended to run 'sudo relinux clean' (without quotes)"\
" once you have burned and tested $CUSTOMISO"
  echo "It will not delete $CUSTOMISO or $CUSTOMISO.md5"
  echo
}

if [[ "$1" == "clean" ]]
then
  echo "Cleaning..."
  rm -rf $WORKDIR/dummysys > /dev/null 2>&1
  rm -rf $WORKDIR/ISOTMP > /dev/null 2>&1
  rm -f $WORKDIR/.filesystem.squashfs.* > /dev/null 2>&1
  echo "Done"
  exit 0
fi
if [[ "$1" == "fullclean" ]]
then
  echo "Cleaning..."
  rm -rf $WORKDIR
  echo "Done"
  exit 0
fi
if [[ "$1" == "squashfs" ]]
then
  cdfs $@
  exit 0
fi
if [[ "$1" == "onlyiso" ]]
then
  iso $@
  exit 0
fi
if [[ "$1" == "config" ]]
then
  echo "Generating configuration file..."
  cp /etc/relinux/relinux.conf ./relinux.conf
  sed -i \
's:EXCLUDES="\(.*\)":EXCLUDES="\1 '`readlink -f ./relinux.conf`'":g' \
./relinux.conf
  chmod 0666 ./relinux.conf
  echo "Done. File is located here: `readlink -f ./relinux.conf`"
  exit 0
fi
cdfs $@
iso $@
exit 0
